package com.risenb.service.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.risenb.bean.Page;
import com.risenb.bean.SystemLog;
import com.risenb.constant.DateFormat;
import com.risenb.dao.ManagerMapper;
import com.risenb.dao.SystemLogMapper;
import com.risenb.service.SystemLogService;
import com.risenb.util.DateUtil;
import com.risenb.util.ExcelUtil;

@Service
public class SystemLogServiceImpl implements SystemLogService {
	
	@Autowired
	SystemLogMapper systemLogMapper;
	
	@Autowired
	ManagerMapper managerMapper;

	@Override
	public int removeSystemLogById(Integer id) {
		return systemLogMapper.removeSystemLogById(id);
	}

	@Override
	public int createSystemLog(SystemLog record) {
		return systemLogMapper.createSystemLog(record);
	}

	@Override
	public SystemLog querySystemLogById(Integer id) {
		return systemLogMapper.querySystemLogById(id);
	}

	@Override
	public int modifySystemLogById(SystemLog record) {
		return systemLogMapper.modifySystemLogById(record);
	}

	@Override
	public List<SystemLog> queryAllByPage(Page<SystemLog> page) {
		List<SystemLog> sl = systemLogMapper.queryAllByPage(page);
		for (int i = 0; i < sl.size(); i++) {
			sl.get(i).setOperatorM(managerMapper.queryManagerById(sl.get(i).getOperator()));
		}
		return sl;
	}

	@Override
	public void exportExcel(List<SystemLog> systemlog, String[] titles, ServletOutputStream outputStream) {
		XSSFWorkbook workBook = new XSSFWorkbook();
		// 在workbook中添加一个sheet,对应Excel文件中的sheet
		XSSFSheet sheet = workBook.createSheet("系统日志");
		ExcelUtil exportUtil = new ExcelUtil(workBook, sheet);
		XSSFCellStyle headStyle = exportUtil.getHeadStyle();
		XSSFCellStyle bodyStyle = exportUtil.getBodyStyle();
		// 构建表头
		XSSFRow headRow = sheet.createRow(0);
		XSSFCell cell = null;
		for (int i = 0; i < titles.length; i++) {
			cell = headRow.createCell(i);
			cell.setCellStyle(headStyle);
			cell.setCellValue(titles[i]);
		}
		// 构建表体数据
		if (systemlog != null && systemlog.size() > 0) {
			for (int j = 0; j < systemlog.size(); j++) {
				XSSFRow bodyRow = sheet.createRow(j + 1);
				SystemLog sl = systemlog.get(j);

				cell = bodyRow.createCell(0);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(sl.getTitle());

				cell = bodyRow.createCell(1);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(sl.getContent());

				cell = bodyRow.createCell(2);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(DateUtil.format(sl.getOperatime(), DateFormat.DEFAULT_FORMAT));

				cell = bodyRow.createCell(3);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(sl.getStatus() == 0 ? "成功" : "失败");

				cell = bodyRow.createCell(4);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(sl.getOperatorM().getUsername());
			}
		}
		try {
			workBook.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<SystemLog> queryAll() {
		List<SystemLog> sl = systemLogMapper.queryAlls();
		for (int i = 0; i < sl.size(); i++) {
			sl.get(i).setOperatorM(managerMapper.queryManagerById(sl.get(i).getOperator()));
		}
		return sl;
	}

	@Override
	public List<SystemLog> queryAll(List<String> expids) {
		List<SystemLog> sl = systemLogMapper.queryAll(expids);
		for (int i = 0; i < sl.size(); i++) {
			sl.get(i).setOperatorM(managerMapper.queryManagerById(sl.get(i).getOperator()));
		}
		return sl;
	}

}
